//
//  SelectedMonthView.m
//  PropertyMananger
//
//  Created by bh on 17/1/16.
//  Copyright © 2017年 BH. All rights reserved.
//

#import "SelectedMonthView.h"
#import "SelectedMonthCell.h"

@interface SelectedMonthView ()<UICollectionViewDelegate,UICollectionViewDataSource>

@end

@implementation SelectedMonthView
{
      UIWindow * window;
}
 static NSString * SelectedCellID = @"SelectedCellID";

- (instancetype)initWithFrame:(CGRect)frame
{

    self = [super initWithFrame:frame];
    if (self) {
        
        [self createMonthUI];
        
        self.isSelected = YES;
    }
    return self;
}
- (void)createMonthUI
{
    self.monthArray = [[NSMutableArray alloc] initWithObjects:@"1月",@"2月",@"3月",@"4月",@"5月",@"6月",@"7月",@"8月",@"9月",@"10月",@"11月",@"12月", nil];
    
    UIView * bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:bgView];
    bgView.layer.cornerRadius = 10;
    bgView.layer.masksToBounds = YES;
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self).with.offset(0);
        make.centerY.equalTo(self).with.offset(0);
        make.size.mas_equalTo(CGSizeMake(270,180));
    }];
    
    UILabel * titleLabel = [[UILabel alloc]init];
    [self addSubview:titleLabel];
    
    titleLabel.text = @"选择月份";
    titleLabel.textColor = BHColor(102, 102, 102);
    titleLabel.font = BHFont(13);
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self).with.offset(7.5);
        make.top.equalTo(self).with.offset(15);
        make.size.mas_equalTo(CGSizeMake(60, 25));
    }];
    
    UIView * lineView = [[UIView alloc]init];
    [self addSubview:lineView];
    
    lineView.backgroundColor = BHColor(230, 230, 230);
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self).with.offset(0);
        make.top.equalTo(titleLabel.mas_bottom).with.offset(7.5);
        make.trailing.equalTo(self).with.offset(0);
        make.height.mas_equalTo(1);
    }];
    
    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(50, 30);
    flowLayout.minimumLineSpacing = 5;
    flowLayout.minimumInteritemSpacing = 5;
    // 设置组的内边距
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 30, 0, 0);

   self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50, 250, 160) collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    [self.collectionView registerClass:[SelectedMonthCell class] forCellWithReuseIdentifier:SelectedCellID];
    [bgView addSubview:self.collectionView];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.monthArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    SelectedMonthCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:SelectedCellID forIndexPath:indexPath];
    
    cell.monthLabel.text =self.monthArray[indexPath.item]; 
    
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"item"] != nil) {
        int item = [[[NSUserDefaults standardUserDefaults] objectForKey:@"item"] intValue];
        
        if ([cell.monthLabel.text isEqualToString:self.monthArray[item]]) {
            cell.monthLabel.backgroundColor = Theme_Color;
            cell.monthLabel.textColor = [UIColor whiteColor];
        }
        
    }else{
        if ([cell.monthLabel.text isEqualToString:[NSString stringWithFormat:@"%ld",(long)[self date]]]) {
            cell.monthLabel.backgroundColor = Theme_Color;
            cell.monthLabel.textColor = [UIColor whiteColor];
        }
    }
    return cell;

}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectMonth:)]) {
        [self.delegate didSelectMonth:indexPath.item];
        NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setObject:[NSString stringWithFormat:@"%ld",(long)indexPath.item] forKey:@"item"];
        [userDefaults synchronize];
    }
    [self dismissSelectedView];
}
//获取当前月份
- (NSInteger)date{
    NSDate * date = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:date];
    return [dateComponent month];
}
- (void)selectedMonthView{
    
    window = [[UIWindow alloc] init];
    CGRect rect = [UIScreen mainScreen].bounds;
    [window makeKeyAndVisible];
    window.frame = rect;
    window.windowLevel = UIWindowLevelAlert;
    [window addSubview:self];
    window.backgroundColor = [UIColor colorWithHue:0 saturation:0 brightness:0 alpha:0.5];
    UIView * bgView = [[UIView alloc] initWithFrame:rect];
    [window addSubview:bgView];
    [window bringSubviewToFront:self];
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    [bgView addGestureRecognizer:tap];
    
}
- (void)tap:(UITapGestureRecognizer *)tap{

    [self dismissSelectedView];

}
- (void)dismissSelectedView
{
    [self removeFromSuperview];
    window.windowLevel = UIWindowLevelNormal;
    [window resignKeyWindow];
    window.backgroundColor = [UIColor clearColor];
    window = nil;
}


@end
